﻿#pragma once
#include "NCKHDemo.h"
#include "SignalForm.h"
namespace NCKHDemo {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Threading;
	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
			imgin = NULL;
			signalForm = gcnew SignalForm();
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: String ^openFileName;
	private: int M, N;
	private:	IplImage *imgin;
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	protected: 
	private: System::Windows::Forms::ToolStripMenuItem^  menuToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  openToolStripMenuItem;
	private: System::Windows::Forms::PictureBox^  pictureBox;
	private: System::Windows::Forms::ToolStripMenuItem^  trainingToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  taoCSDLToolStripMenuItem;
	private: System::Windows::Forms::FolderBrowserDialog^  folderBrowserDialog1;
	private: System::Windows::Forms::ToolStripMenuItem^  pCASVMToolStripMenuItem;
	private: System::Windows::Forms::Label^  lblThongBao;
	private: System::Windows::Forms::ToolStripMenuItem^  khoiDongNhanDangToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  openVideoAVIToolStripMenuItem;



	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->menuToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->openToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->khoiDongNhanDangToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->trainingToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->taoCSDLToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->pCASVMToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->pictureBox = (gcnew System::Windows::Forms::PictureBox());
			this->folderBrowserDialog1 = (gcnew System::Windows::Forms::FolderBrowserDialog());
			this->lblThongBao = (gcnew System::Windows::Forms::Label());
			this->openVideoAVIToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->menuStrip1->SuspendLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox))->BeginInit();
			this->SuspendLayout();
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->menuToolStripMenuItem, 
				this->trainingToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(284, 24);
			this->menuStrip1->TabIndex = 0;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// menuToolStripMenuItem
			// 
			this->menuToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->openToolStripMenuItem, 
				this->openVideoAVIToolStripMenuItem, this->khoiDongNhanDangToolStripMenuItem});
			this->menuToolStripMenuItem->Name = L"menuToolStripMenuItem";
			this->menuToolStripMenuItem->Size = System::Drawing::Size(50, 20);
			this->menuToolStripMenuItem->Text = L"Menu";
			// 
			// openToolStripMenuItem
			// 
			this->openToolStripMenuItem->Name = L"openToolStripMenuItem";
			this->openToolStripMenuItem->Size = System::Drawing::Size(189, 22);
			this->openToolStripMenuItem->Text = L"Open Image";
			this->openToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::openToolStripMenuItem_Click);
			// 
			// khoiDongNhanDangToolStripMenuItem
			// 
			this->khoiDongNhanDangToolStripMenuItem->Name = L"khoiDongNhanDangToolStripMenuItem";
			this->khoiDongNhanDangToolStripMenuItem->Size = System::Drawing::Size(189, 22);
			this->khoiDongNhanDangToolStripMenuItem->Text = L"Khoi dong nhan dang";
			this->khoiDongNhanDangToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::khoiDongNhanDangToolStripMenuItem_Click);
			// 
			// trainingToolStripMenuItem
			// 
			this->trainingToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->taoCSDLToolStripMenuItem, 
				this->pCASVMToolStripMenuItem});
			this->trainingToolStripMenuItem->Name = L"trainingToolStripMenuItem";
			this->trainingToolStripMenuItem->Size = System::Drawing::Size(63, 20);
			this->trainingToolStripMenuItem->Text = L"Training";
			// 
			// taoCSDLToolStripMenuItem
			// 
			this->taoCSDLToolStripMenuItem->Name = L"taoCSDLToolStripMenuItem";
			this->taoCSDLToolStripMenuItem->Size = System::Drawing::Size(126, 22);
			this->taoCSDLToolStripMenuItem->Text = L"Tao CSDL";
			this->taoCSDLToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::taoCSDLToolStripMenuItem_Click);
			// 
			// pCASVMToolStripMenuItem
			// 
			this->pCASVMToolStripMenuItem->Name = L"pCASVMToolStripMenuItem";
			this->pCASVMToolStripMenuItem->Size = System::Drawing::Size(126, 22);
			this->pCASVMToolStripMenuItem->Text = L"PCA_SVM";
			this->pCASVMToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::pCASVMToolStripMenuItem_Click);
			// 
			// pictureBox
			// 
			this->pictureBox->Location = System::Drawing::Point(0, 24);
			this->pictureBox->Name = L"pictureBox";
			this->pictureBox->Size = System::Drawing::Size(128, 113);
			this->pictureBox->SizeMode = System::Windows::Forms::PictureBoxSizeMode::AutoSize;
			this->pictureBox->TabIndex = 1;
			this->pictureBox->TabStop = false;
			// 
			// lblThongBao
			// 
			this->lblThongBao->AutoSize = true;
			this->lblThongBao->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(163)));
			this->lblThongBao->Location = System::Drawing::Point(259, 24);
			this->lblThongBao->Name = L"lblThongBao";
			this->lblThongBao->Size = System::Drawing::Size(0, 31);
			this->lblThongBao->TabIndex = 2;
			this->lblThongBao->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
			// 
			// openVideoAVIToolStripMenuItem
			// 
			this->openVideoAVIToolStripMenuItem->Name = L"openVideoAVIToolStripMenuItem";
			this->openVideoAVIToolStripMenuItem->Size = System::Drawing::Size(189, 22);
			this->openVideoAVIToolStripMenuItem->Text = L"Open video AVI";
			this->openVideoAVIToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::openVideoAVIToolStripMenuItem_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->AutoSize = true;
			this->ClientSize = System::Drawing::Size(284, 262);
			this->Controls->Add(this->lblThongBao);
			this->Controls->Add(this->pictureBox);
			this->Controls->Add(this->menuStrip1);
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->WindowState = System::Windows::Forms::FormWindowState::Maximized;
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion


	
	private: SignalForm^ signalForm;
		/* Cac ham can thiet */
	private: static  Bitmap^ bm;
	public: void VeLenPictureBox(IplImage *anhVeLenPictureBox) 
				{
					if(anhVeLenPictureBox!=NULL)
					{
						DrawCvImage(anhVeLenPictureBox,pictureBox);

						bm = gcnew Bitmap( pictureBox->Image);
					}
				}
	private: void DrawCvImage(IplImage *CvImage,PictureBox^ pictureBoxOut) 
			 {
				 

					 /////////////////////
					 Bitmap^ bmp;			 
					 /////////////////////////////////// 
					 if ((pictureBoxOut->Image == nullptr) || (pictureBoxOut->Width != CvImage->width) || (pictureBoxOut->Height != CvImage->height))
					 {
						 Bitmap^ bmpPicBox = gcnew Bitmap(CvImage->width, CvImage->height);	
						 pictureBoxOut->Image = bmpPicBox; 
					 }

					 //Graphics
					 Graphics^g = Graphics::FromImage(pictureBoxOut->Image); 

					 /////////////////////////////////// 			 
					 if ((CvImage->depth == IPL_DEPTH_8U) && (CvImage->nChannels == 3))
					 {
						 bmp = gcnew Bitmap(CvImage->width, CvImage->height, CvImage->widthStep,
							 System::Drawing::Imaging::PixelFormat::Format24bppRgb, IntPtr(CvImage->imageData)); 
					 }
					 else if ((CvImage->depth == IPL_DEPTH_8U) && (CvImage->nChannels == 1))
					 {	 
						 bmp = gcnew Bitmap(CvImage->width, CvImage->height, CvImage->widthStep,
							 System::Drawing::Imaging::PixelFormat::Format8bppIndexed, IntPtr(CvImage->imageData)); 

						 Imaging::ColorPalette^ pal = bmp->Palette;
						 for (int i = 0; i < 256; i++)
						 {
							 pal->Entries[i] = Color::FromArgb(255, i, i, i);
						 }
						 bmp->Palette = pal;
					 }
					 else
					 {
						 MessageBox::Show("Your Code doesn't work!");
						 delete g;
						 return;
					 } 
					 /////////////////////////////////// 
					 g->DrawImage(bmp, 0, 0, CvImage->width, CvImage->height);  
					 pictureBoxOut->Refresh();

					 delete g;	/////////////////
					 delete bmp;
				 
			 }
			 			 /*Hàm hiện cửa sổ quan sát SignalForm*/
	public: void HienCuaSoQuanSat() 
			{
				signalForm->Show();
				
			}
						/*Hàm ẩn cửa sổ quan sát SignalForm*/
	public: void AnCuaSoQuanSat() 
			{
				signalForm->Hide();
			}
			
	public: IplImage* skipNFrames(CvCapture* capture, int n)
			{
				for(int i = 0; i < n; ++i)
				{
					if(cvQueryFrame(capture) == NULL)
					{
						return NULL;
					}
				}
				return cvQueryFrame(capture);
			}
	
		/*---------------*/
	private: System::Void openToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
					OpenFileDialog ^dlg = gcnew OpenFileDialog();
					dlg->Title = "Open Image";
					dlg->Filter = L"Bitmap files (*.bmp; *.jpg; *.jpeg; *.tif; *.gif; *.png) | *.bmp; *.jpg; *.jpeg; *.tif; *.gif; *.png"; 
					if (dlg->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
						openFileName = dlg->FileName;
						char cOpenFileName[256];
						sprintf(cOpenFileName,"%s",openFileName);	

						imgin = cvLoadImage(cOpenFileName,1);

						M = imgin->height;
						N = imgin->width;

						IplImage *imgout = NULL;
						imgout = cvCreateImage(cvSize(50,50),8,1);
						bool ck = Co(imgin);
						if (ck) {
							DetectImage(imgin,imgout);
						
							VeLenPictureBox(imgin);
							float res = 0;

							res = DocFileEigenPsi(imgout);
							
							
							String^ thongbao;
							if (res==1)
							{
								thongbao = "Cho ngoat nguy hiem vong ben trai";
						
								char urlImage[255] = "1.bmp";
								signalForm->SetImage(cvLoadImage(urlImage,1));
								signalForm->VeLenPictureBox();
								HienCuaSoQuanSat();
							}
							else if(res == 2)
							{
								thongbao = "Cho ngoat nguy hiem vong ben phai";
							}
							else if(res == 3)
							{
								thongbao = "Giao nhau voi tuyen duong cung cap";
							}
							else if(res == 4)
							{
								thongbao = "Giao nhau voi duong khong uu tien ben phai";
							}
							else if(res == 5)
							{
								thongbao = "Giao nhau voi duong sat co rao chan";
							}
							else if(res == 6)
							{
								thongbao = "Nguoi di bo";
							}
							else 
							{
								thongbao = "Khong xac dinh kich thuoc HxW" + M + "x" + N;
							}
							lblThongBao->Text = thongbao;
						}
						else
						{
							VeLenPictureBox(imgin);
							lblThongBao->Text = "";
						}
					}
			 }
	private: System::Void taoCSDLToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
				 
				 if(folderBrowserDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
				 {
					 char cOpenFileName[256];
					 //cOpenFileName = folderBrowserDialog1->SelectedPath;
					 sprintf(cOpenFileName,"%s",folderBrowserDialog1->SelectedPath);

					 TaoCSDLHoc(cOpenFileName);
				 }
				
			 }
	private: System::Void pCASVMToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 GhiFileEigenPsi();
		 }
	private: System::Void khoiDongNhanDangToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 DocFileEigen();
			 DocFilePsi();
		 }
	private: System::Void openVideoAVIToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			OpenFileDialog ^dlg = gcnew OpenFileDialog();
			dlg->Title = "Open Video";
			dlg->Filter = L"AVI (*.avi;) | *.avi;"; 
			 if(dlg->ShowDialog() == System::Windows::Forms::DialogResult::OK)
				 {
					 char cOpenFileName[256];
					 //cOpenFileName = folderBrowserDialog1->SelectedPath;
					 sprintf(cOpenFileName,"%s",dlg->FileName);
					 //DetectVideo(cOpenFileName);


					 	CvCapture *capture;
						IplImage  *frame;
						IplImage  *frame2 = cvCreateImage( cvSize(50,50), 8, 1 );
						int       key=0;

						char buffer[255];
						CvFont font;
						cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX,0.5,0.5);

						capture = cvCaptureFromAVI(cOpenFileName);
						cvNamedWindow( "video", 1 );



						while( key != 'q' ) {
							frame = skipNFrames(capture,1);//cvQueryFrame( capture );

							if( !frame ) {
								return;
							}

							frame->origin = 0;
		
							bool ck = Co(frame);
							if(!ck) 
							{
								key = cvWaitKey( 10 );
								cvShowImage( "video", frame );	

							}
							else
							{
								DetectImage( frame,frame2 );
								//System::String^ name = gcnew System::String("D:\\APPLAUSE.WAV");
								//	//create an instance of the soundplayer class, sndPlayer can be a different name
								//	System::Media::SoundPlayer sndPlayer(name);
								//	sndPlayer.Play(); //there is also Stop(), and PlayLooping()
								key = cvWaitKey( 10 );
								float res = 0;
								res = DocFileEigenPsi(frame2);
								if (res == 1.0)
										sprintf(buffer,"Cho ngoat nguy hiem vong ben trai");
									else if (res == 2.0)
										sprintf(buffer,"Cho ngoat nguy hiem vong ben phai");
									else if (res == 3.0)
									{
										sprintf(buffer,"Giao nhau voi tuyen duong cung cap");
										char urlImage[255] = "1.bmp";
										signalForm->PlaySound("1.wav");
										signalForm->SetImage(cvLoadImage(urlImage,1));
										
										signalForm->VeLenPictureBox();
										HienCuaSoQuanSat();
									}
									else if (res == 4.0)
									{
										sprintf(buffer,"Giao nhau voi duong khong uu tien ben phai");
										char urlImage[255] = "1.bmp";
										signalForm->SetImage(cvLoadImage(urlImage,1));
										signalForm->VeLenPictureBox();
										signalForm->PlaySound("1.wav");
										HienCuaSoQuanSat();
									}
									else if (res == 5.0)
										sprintf(buffer,"Giao nhau voi duong sat co rao chan");
									else if (res == 6.0)
										sprintf(buffer,"Nguoi di bo");
									else
										sprintf(buffer,"Không xác định");
			
									cvPutText(frame,buffer,cvPoint(50,40),&font,CV_RGB(255,0,0));
									cvShowImage( "video", frame );
							}
					
						}
						cvWaitKey( 0 );
						//cvReleaseImage(&frame);
						cvReleaseCapture( &capture );
						cvDestroyWindow( "video" );
				 }
		}
	};
		
}

